package telco.provider.service;

import java.util.Collection;

import swa2012.telco.broker.beans.Client;
import swa2012.telco.broker.beans.Message;
import swa2012.telco.broker.beans.ReceiveMessageStatus;
import swa2012.telco.broker.BrokerCallback;

import telco.provider.manager.BrokerManager;
import telco.provider.manager.ClientManager;
import telco.provider.manager.MessageManager;

public class BrokerCallbackImpl implements BrokerCallback {
	
	//Constructor	
	public void initIt() {
		
	}
	
	/*
	 * Web Service cleanup
	 * - Deregister from broker
	 * - Deregister all clients
	 * - Delete all undelivered messages
	 */
	public void cleanUp()
	{
		BrokerManager.getInstance().deregisterMe();
		ClientManager.getInstance().deregisterAllClients();
		MessageManager.getInstance().deleteAllMessages();
	}

	/*
	 * BrokerCallbackImpl
	 */
	public ReceiveMessageStatus receiveMessage(Message msg) {
		try
		{
			MessageManager.getInstance().sendMessage(msg);			
			return ReceiveMessageStatus.RECEIVE_MESSAGE_OK;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return ReceiveMessageStatus.RECEIVE_MESSAGE_ERROR;
		}
	}

	public Collection<Client> listClients() {
		return ClientManager.getInstance().getClients();
	}

	public boolean ping() {
		return true;
	}
}
